package pl.edu.agh.student.logic.webapp;

import java.io.Serializable;

import javax.ejb.Remove;
import javax.ejb.Stateful;

import org.apache.axis2.AxisFault;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.faces.FacesMessages;

import pl.edu.agh.student.logic.bik.BikServiceImpl;
import pl.edu.agh.student.logic.util.EndpointProvider;

@Stateful
@Name("bikBean")
@Scope(ScopeType.CONVERSATION)
@Transactional
public class BikBeanImpl implements BikBean, Serializable {

	private static final long serialVersionUID = 31808303745954934L;
	private String bikEndpoint;

	@In("endpointProvider")
	EndpointProvider endpointProvider;

	@In(value = "bikService", create = true, required = false)
	@Out
	protected BikServiceImpl bikService;

	@In(create=true)
	private FacesMessages facesMessages;

	@In("taskBean")
	protected TaskBean taskBean;

	public void getCustomerObligations() {
		System.out.println("BikBeanImpl.getCustomerObligations(): "
				+ taskBean.getApplication(taskBean.getCurrentTask()).getName());
		try {
			bikService.getCustomerObligations(
					taskBean.getApplication(taskBean.getCurrentTask()),
					bikEndpoint);
		} catch (AxisFault e) {
			facesMessages.addFromResourceBundle("bik.failed");
			e.printStackTrace();
		}
	}

	@Destroy
	@Remove
	public void destroy() {
	}

	public String getBikEndpoint() {
		if(bikEndpoint == null)
			bikEndpoint = endpointProvider.getBikEndpoint();
		return bikEndpoint;
	}

	public void setBikEndpoint(String bikEndpoint) {
		this.bikEndpoint = bikEndpoint;
	}
}
